import axios from 'axios';
import { getItem } from '@/utils/stroge'


// 创建第一个实例
const instance1 = axios.create({
    timeout: 5000,
    // baseURL: 'https://tax-robot-server.kjgctest.k5.bigtree.tech',
    baseURL: '/api'
});

// 实例的请求拦截器
instance1.interceptors.request.use(
    config => {
        config.headers.Authorization = getItem('token')
        return config;
    },
    error => {
        return Promise.reject(error);
    }
);
 
// 实例的响应拦截器
instance1.interceptors.response.use(
    response => {
        return response;
    },
    error => {
        return Promise.reject(error)
    }
);
 
/**
 * 封装get方法
 * @param url
 * @param data
 * @returns {Promise}
 */
export function get(url, params = {}) {
    return new Promise((resolve, reject) => {
        instance1.get(url, {
                params: params
            })
            .then(response => {
                resolve(response.data);
            })
            .catch(err => {
                reject(err)
            })
    })
}
 
/**
 * 封装post请求
 * @param url
 * @param data
 * @returns {Promise}
 */
export function post(url, data = {}) {
    return new Promise((resolve, reject) => {
        instance1.post(url, data)
            .then(response => {
                resolve(response.data);
            }, err => {
                reject(err)
            })
    })
}